{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字符串和格式化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 1、字符串方法\n",
    "\n",
    "|方法|描述|\n",
    "|----|----|\n",
    "|s.capitalize()                          |第一个字母变大写。例如：'abc'.capitalize()  #'Abc'|\n",
    "|s.count(sub [,start [,end ]])           |子串sub出现的次数。例如：'abaab'.count('ab') #2|\n",
    "|s.encode([encoding [,errors ]])         |改变字符串的编码   |             \n",
    "|s.startswith(prefix [,start [,end ]])   |检查字符串的开头是否为prefix。例如：'yejinlei'.startswith('ye') #True|\n",
    "|s.endswith(suffix [,start [,end ]])     |检查字符串的结尾是否是suffix。例如：'yejinlei'.endswith('lei') #True|       \n",
    "|s.expandtabs([tabsize ])                |将制表符转换为一定数量的空格|\n",
    "|s.find(sub [,start [,end ]])            |返回子串 sub 首次出现的位置或者 -1。例如：'yejinlei'.find('jin') #2|\n",
    "|s.rfind(sub [,start [,end ]])           |返回子串 sub 末次出现的位置或者 -1。例如：'yejinlei'.find('jin') #6|\n",
    "|s.index(sub [,start [,end ]])           |返回子串 sub 首次出现的位置或者引起异常ValueError: substring not found|\n",
    "|s.rindex(sub [,start [,end ]])          |返回子串 sub 末次出现的位置或者引发异常ValueError: substring not found|\n",
    "|s.isalnum()                             |字符是否都为字母或数字|\n",
    "|s.isalpha()                             |字符是否都为字母|\n",
    "|s.isdigit()                             |字符是否都为数字|\n",
    "|s.islower()                             |字符是否都为小写|\n",
    "|s.isspace()                             |字符是否都为空白|\n",
    "|s.istitle()                             |检查字符是否为标题格式(每个单词的第一个字母大写)|\n",
    "|s.isupper()                             |字符是否都为大写|\n",
    "|s.join(t)                               |用 s 连接 t 中的所有字符串。例如：','.join(['hello','yejinlei']) #'hello,yejinlei'|\n",
    "|s.center(width)                         |在长度为 width 范围内将字符串置中。|\n",
    "|s.ljust(width)                         |在宽度为 width 内左对齐|\n",
    "|s.rjust(width)                         |在宽度为 width 内右对齐|\n",
    "|s.lower()                               |s 中所有字符小写|\n",
    "|s.upper()                               |s 中所有字符大写|\n",
    "|s.replace(old , new [,maxreplace ])     |将子串 old 替换为 new。例如：'hello,yejinlei'.replace('hello',u'你好') #'你好,yejinlei'|\n",
    "|s.lstrip()                              |删去字符串s开头的空白|\n",
    "|s.rstrip()                              |删去字符串s末尾的空白|\n",
    "|s.strip()                               |删去字符串s开头和末尾的空白|\n",
    "|s.split([sep [,maxsplit ]])             |将字符串 s 分割成一个字符串列表，其中 sep 为分隔符，maxsplit是最大分割次数。例如：'hello@yejinlei'.split('@') #['hello', 'yejinlei']|\n",
    "|s.splitlines([keepends ])               |将字符串按行分割为一个字符串列表，若keepends为1，则保留换行符'\\n'。例如：'hello\\nyejinlei'.splitlines() #['hello', 'yejinlei']|\n",
    "|s.swapcase()                            |串内字符大小写互换。例如：'yEJINLEI'.swapcase() #‘Yejinlei’|\n",
    "|s.title()                               |s 转换为标题格式(每个单词的第一个字母大写)。例如：'hello\\nyejinlei'.title() #'Hello\\nYejinlei'|\n",
    "|s.translate(table [,deletechars ])      |使用字符转换表转换一个字符串|\n",
    "|s.format(*args, **kwargs)      |格式化字符串，下面重点介绍|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 2、格式化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1221\n",
      "12\n",
      "yejinlei\n",
      "yejinlei\n",
      "Im apple!\n",
      "1,3\n",
      "|左       |\n",
      "|   中    |\n",
      "|       右|\n",
      "|___發____|\n",
      "123.46\n",
      "1010\n",
      "10\n",
      "12\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "#{num}通过位置\n",
    "print '{0}{1}{1}{0}'.format(1, 2)\n",
    "print '{}{}'.format(1, 2)\n",
    "\n",
    "#{name}通过名字\n",
    "print '{last}{first}'.format(first='jinlei', last='ye')\n",
    "print '{first}{last}'.format(last='jinlei', first='ye')\n",
    "\n",
    "#self.属性\n",
    "class Apple:\n",
    "    def __init__(self,a,b):\n",
    "        self.a = a\n",
    "        self.b = b\n",
    "    def __str__(self):\n",
    "        return '{self.a}{self.b}'.format(self=self)\n",
    "apple = Apple(r'Im ','apple!')\n",
    "print apple\n",
    "\n",
    "#通过下标\n",
    "l=['1','2','3']\n",
    "print '{0[0]},{0[2]}'.format(l)\n",
    "\n",
    "#格式限定符\n",
    "##^、<、>分别是居中、左对齐、右对齐，后面带宽度\n",
    "##:号后面带填充的字符，只能是一个字符，不指定的话默认是用空格填充\n",
    "print '|{:<10}|'.format('左')\n",
    "print '|{:^10}|'.format('中')\n",
    "print '|{:>10}|'.format('右')\n",
    "print '|{:_^10}|'.format('發')\n",
    "##精度.M\n",
    "print '{:1.2f}'.format(123.456)\n",
    "##制式b、d、o、x分别是二进制、十进制、八进制、十六进制\n",
    "print '{:b}'.format(10)\n",
    "print '{:d}'.format(10)\n",
    "print '{:o}'.format(10)\n",
    "print '{:x}'.format(10)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
